﻿#encoding=utf-8

#需要导出的公共配置文件(客户端,服务器都需要)
EXPORT_FILES = [
"全局配置表.xlsx",
"地图配置表.xlsx",
"单位配置表.xlsx",
"道具配置表.xlsx",
"掉落组配置表.xlsx",
"BUFF配置表.xlsx",
"技能配置表.xlsx",
"投射物配置表.xlsx",
"AI配置表.xlsx",
"多语言配置表.xlsx"
]

#客户端额外需要导出的额外配置文件(仅客户端需要)
EXPORT_CLIENT_ONLY = [
]

#服务器端额外需要导出的配置文件(仅服务器需要)
EXPORT_SERVER_ONLY = [

]

#GMT需要读取的文本信息
EXPORT_GMT_ONLY = [

]

# do not modify the following

import os
import platform
import traceback
import shutil
import sys

exportscript = '..\\tools/proton.py'     
pythonpath = '..\\tools\\Python\\Python33\\python.exe ' if platform.system() == 'Windows' else 'python '

class ExportError(Exception):
    pass

def export(filelist, format, sign, outfolder, suffix, schema):
    cmd = r' -p "' + ' '.join(filelist) + '" -f ' + outfolder + ' -e ' + format + ' -s ' + sign
    if suffix:
        cmd += ' -t ' + suffix
    if schema:
        cmd += ' -c ' + schema
    cmd = pythonpath + exportscript + cmd
    code = os.system(cmd)
    if code != 0:
        raise ExportError('export excel fail, please see print')

def codegenerator(schema, outfolder, namespace, suffix, hasCSharpLua):
    if os.path.exists(schema):
        cmd = '..\\tools\\CSharpGeneratorForProton\\CSharpGeneratorForProton.exe ' + '-n ' + namespace + ' -f ' + outfolder + ' -p ' + schema
        if suffix:
            cmd += ' -t ' + suffix 
        if hasCSharpLua:
            cmd += ' -csl'
        code = os.system(cmd)
        os.remove(schema)      
        if code != 0:
            raise ExportError('codegenerator fail, please see print')
        
def exportserver():
    export(EXPORT_FILES + EXPORT_SERVER_ONLY, 'json', 'server', 'config_server', 'Template', 'schemaserver.json')
    codegenerator('schemaserver.json', 'config_server/ConfigGenerator/Template', 'Legion.Template', 'Template', False) 
    
def exportclient():
    export(EXPORT_FILES + EXPORT_CLIENT_ONLY, 'json', 'client', 'config_client', 'Template', 'schemaclient.json')
    codegenerator('schemaclient.json', 'config_client/ConfigGenerator/Template', 'Legion.Template', 'Template', True) 
    
def exportGmt():
    export(EXPORT_GMT_ONLY, 'json', 'gmt', 'config_gmt', 'Template', None)  
    
    
def main():
    try:
        exportserver()
        exportclient()
        #exportGmt()
        print("all operation finish successful")
        return 0
    except ExportError as e:
        print(e)
        print("has error, see logs, please return key to exit")
        input()
        return 1
    except Exception as e:
        traceback.print_exc()
        print("has error, see logs, please return key to exit")
        input()
        return 1
        
if __name__ == '__main__':
    sys.exit(main())